package com.example.stretching;

import java.util.Calendar;

import com.example.db.DbopenHelper;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.Animation.AnimationListener;
import android.widget.ImageView;
import android.widget.Button;
import android.widget.TextView;

public class AnimationPage extends Activity implements OnClickListener {

	private Handler mHandler;
	private Runnable mRunnable;

	private int imageIndex = 0;
	private int mDirect = 0;
	private ImageView mImgView01, mImgView02;
	private ImageView mImgView03, mImgView04;
	// private ImageView mImgView03;
	private ImageView mImageAni;

	private Button btnStart, btnlist;
	private Button btnStop;
	private TextView textTimer;
	private CounterDown timer;
	private boolean timerPause = false;

	private Animation mTranslateLeftAnim;
	private Animation mTranslateRightAnim;

	private long startTime = 17 * 1000;
	private final long interval = 1 * 1000;
	
	private String msg;
	private String mode;
	private DbopenHelper mDbOpenHelper;

//	static boolean complete = false;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_animation);

		
//				
//				if(complete = true){
//					String a = "Completed";
////					Log.d("complete", "bb");
////
//				Intent intent = new Intent(this, modifyScheduleActivity.class);
//			      intent.putExtra("complete", a);
//			      }
//				else if(complete = false){
//					String a = "";
//					Intent in = new Intent(this, modifyScheduleActivity.class);
//			      in.putExtra("complete", a);
//				};
		Intent i = getIntent();
		i.getIntExtra("hour", 0);
//		msg = i.getStringExtra("message");
//		mode = i.getStringExtra("mode");
//		
//		System.out.println("@@@@@@@@@@@@@@@@@ = "+msg);
//		System.out.println("@@@@@@@@@@@@@@@@@ = "+mode);

		btnStart = (Button) findViewById(R.id.btn1);
		btnStop = (Button) findViewById(R.id.btn2);
		btnlist = (Button)findViewById(R.id.btn3);
		btnlist.setVisibility(View.GONE);
//		if(complete = true){
//			btnlist.setVisibility(View.VISIBLE);}
//		else if(complete = false){
//			btnlist.setVisibility(View.INVISIBLE);
//		};
		textTimer = (TextView) findViewById(R.id.textTimer);
		if(i.getIntExtra("hour", 0) %2 == 0){
		mImgView01 = (ImageView) findViewById(R.id.imageView1);
		mImgView02 = (ImageView) findViewById(R.id.imageView2);
		
		mImageAni = (ImageView) findViewById(R.id.animationImage);
		}else if(i.getIntExtra("hour", 0) %2 == 1){
			mImgView03 = (ImageView) findViewById(R.id.imageView3);
			mImgView04 = (ImageView) findViewById(R.id.imageView4);
			
			mImageAni = (ImageView) findViewById(R.id.animationImage);
		};
		mTranslateLeftAnim = AnimationUtils.loadAnimation(this,
				R.anim.translate_left);
		mTranslateRightAnim = AnimationUtils.loadAnimation(this,
				R.anim.translate_right);

		PageAnimationListener animListener = new PageAnimationListener();

		mTranslateLeftAnim.setAnimationListener(animListener);
		mTranslateRightAnim.setAnimationListener(animListener);

		btnStart.setOnClickListener(this);
		btnStop.setOnClickListener(this);
		btnlist.setOnClickListener(this);
		timer = new CounterDown(startTime, interval);
		
		

		mRunnable = new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				mDirect = 1;
				imageIndex = 1;
				changeImage();
			}
		};

		mHandler = new Handler();
		mHandler.postDelayed(mRunnable, 2000);

	}

	@Override
	protected void onDestroy() {
		mHandler.removeCallbacks(mRunnable);
		super.onDestroy();
	}

	public void onWindowFocusChanged(boolean hasFocus) {
		// TODO Auto-generated method stub

		mImageAni.setBackgroundResource(R.layout.simple_animation);

		AnimationDrawable frameAnimation = (AnimationDrawable) mImageAni
				.getBackground();
		if (!hasFocus) {

			frameAnimation.start();
		} else {

			frameAnimation.stop();
		}
		super.onWindowFocusChanged(hasFocus);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		int get_id = (v.getId());
		switch (get_id) {
		case R.id.btn3:
			
			tabMainActivity.time_tab = System.currentTimeMillis();
			String a = "Completed";
			Intent intent0 = new Intent(AnimationPage.this, tabMainActivity.class);
			intent0.putExtra("complete", a);
			intent0.putExtra("time", System.currentTimeMillis());
			Log.d("time", Integer.toString((int) System.currentTimeMillis()));
			startActivity(intent0);
//			Intent intent9 = new Intent(AnimationPage.this, Custom_List_Adapter.class);
//			intent9.putExtra("complete", a);
//			startActivity(intent9);
			
			 try { Thread.sleep(500);
             } catch (InterruptedException e) {
                 e.printStackTrace();
             }
			 finish();
		case R.id.btn1:
			if (!timerPause) {
				btnStart.setText("pause");
				this.onWindowFocusChanged(false);
				timer.start();
				mDirect = 1;

				imageIndex = 2;
				changeImage();

				timerPause = true;
				System.out.println("############start");
			} else {
				btnStart.setText("start");
				this.onWindowFocusChanged(true);// 이미지 변환 stop...start버튼누르면 다시
												// 화면 바뀜
				String[] timeStr;
				timeStr = textTimer.getText().toString().split(":");
				timer.cancel();
				startTime = Long.parseLong(timeStr[1]);
				timer = new CounterDown(startTime, interval);
				textTimer.setText("00:" + Long.parseLong(timeStr[1]));
				System.out.println(Long.parseLong(timeStr[1]));
				timerPause = false;
				System.out.println("############pause");
			}
			break;

		case R.id.btn2:
			this.onWindowFocusChanged(true);
			timer.cancel();
			mDirect = 0;

			imageIndex = 1;
			textTimer.setText("00:17");

			mImgView02.bringToFront();
			mImgView02.invalidate();
			mImgView02.setVisibility(View.VISIBLE);
			btnStart.setText("start");
			timerPause = false;
			System.out.println("############stop");
			break;
		}
	}

	public class CounterDown extends CountDownTimer {

		public CounterDown(long millisInFuture, long countDownInterval) {
			super(millisInFuture, countDownInterval);
		}

		@Override
		public void onFinish() {
	
			textTimer.setText("00:00");
			tabMainActivity.complete = true;
			Log.d("complete", "abcd");
			btnlist.setVisibility(View.VISIBLE);
//			Log.d("complete", "bb");
//

		}

		@Override
		public void onTick(long millisUntilFinished) {

			int seconds = (int) (millisUntilFinished / 1000);
			int minutes = seconds / 60;
			seconds = seconds % 60;

			if (minutes >= 10) {
				textTimer.setText("" + minutes + ":"
						+ String.format("%02d", seconds));
			} else {
				textTimer.setText("0" + minutes + ":"
						+ String.format("%02d", seconds));
			}

		}

	}

	private void changeImage() {
		if (imageIndex == 0) {
			mImgView01.bringToFront();
			mImgView01.setVisibility(View.VISIBLE);
			if (mDirect == 0) {
				mImgView01.startAnimation(mTranslateRightAnim);
			} else {
				mImgView01.startAnimation(mTranslateLeftAnim);
			}

		} else if (imageIndex == 1) {
			mImgView02.bringToFront();
			// mImgView02.invalidate();
			mImgView02.setVisibility(View.VISIBLE);
			if (mDirect == 0) {
				mImgView02.startAnimation(mTranslateRightAnim);
			} else {
				mImgView02.startAnimation(mTranslateLeftAnim);
			}

		} else if (imageIndex == 2) {
			mImageAni.bringToFront();
			mImageAni.setVisibility(View.VISIBLE);
			if (mDirect == 0) {
				mImageAni.startAnimation(mTranslateRightAnim);
			} else {
				mImageAni.startAnimation(mTranslateLeftAnim);
			}
		}
		setViewInvalidate(mImgView01, mImgView02, mImageAni);
	}

	
	private void setViewInvalidate(View... views) {
		for (View v : views) {
			v.invalidate();
		}
	}
	
	public class PageAnimationListener implements AnimationListener {

		public void onAnimationEnd(Animation arg0) {
			
			if (imageIndex == 0) {
				mImgView02.setVisibility(View.INVISIBLE);
				mImageAni.setVisibility(View.INVISIBLE);

			} else if (imageIndex == 1) {
				mImgView01.setVisibility(View.INVISIBLE);
				mImageAni.setVisibility(View.INVISIBLE);

			} else if (imageIndex == 2) {
				mImgView01.setVisibility(View.INVISIBLE);
				mImgView02.setVisibility(View.INVISIBLE);
			}

		}

		public void onAnimationRepeat(Animation arg0) {
			// TODO Auto-generated method stub

		}

		public void onAnimationStart(Animation arg0) {
			// TODO Auto-generated method stub

		}

	}

}
